package servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Annual_reportDao;
import dao.Campus_constructionDao;
import dao.Funding_informationDao;
import dao.Fundraising_projectDao;
import dao.ManageDao;
import dao.NewsDao;
import dao.Photo_newsDao;
import dao.ThanksDao;

public class WriteInsertServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String insertTitle = req.getParameter("insertTitle");
		String data = req.getParameter("edt1");

		if (data != null) {
			data = data.replaceAll("\r\n", "");
			data = data.replaceAll("\r", "");
			data = data.replaceAll("\n", "");
			data = data.replaceAll("\"", "'");
		}

		HttpSession session = req.getSession();
		String choice_id2 = (String) session.getAttribute("choice_id");

		int choice_id = Integer.parseInt(choice_id2);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(System.currentTimeMillis());

		int num = 0;
		switch (choice_id) {
		case 1:
			num = new Photo_newsDao().insertItem(insertTitle, data, date);
			break;
		case 2:
			num = new NewsDao().insertItem(insertTitle, data, date);
			break;
		case 3:
			num = new Funding_informationDao().insertItem(insertTitle, data,
					date);
			break;
		case 4:
			break;
		case 5:
			num = new ThanksDao().insertItem(insertTitle, data, date);
			break;
		case 6:
			num = new ManageDao().insertItem(insertTitle, data, date);
			break;
		case 7:
			num = new Annual_reportDao().insertItem(insertTitle, data, date);
			break;
		case 8:
			num = new Fundraising_projectDao().insertItem(insertTitle, data,
					date);
			break;
		case 9:
			num = new Campus_constructionDao().insertItem(insertTitle, data,
					date);
			break;
		}

		if (num == 1) {
			session.setAttribute("find_insert_id", 12);
		} else {
			session.setAttribute("find_insert_id", 13);
		}

		resp.sendRedirect("houtai/guanli_1.jsp?id=" + choice_id);

	}
}
